package com.lilili.ssh.relation.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lilili.ssh.relation.dao.RelationDao;
import com.lilili.ssh.relation.model.Relation;
public class RelationDaoImpl extends HibernateDaoSupport implements RelationDao{
	public void saveRelation(Relation relation){
		if(relation!=null&&StringUtils.isNotEmpty(relation.getId())){
			getHibernateTemplate().saveOrUpdate(relation);
		}else{
			getHibernateTemplate().save(relation);
		}
	}
	
	public void removeRelation(String bookId){
		List<Relation> pojoList=getHibernateTemplate().find("from Relation where book.id='"+bookId+"'");
		for(Relation o:pojoList){
			getHibernateTemplate().delete(o);
		}
	}
	
	public Relation getRelation(String id){
		return (Relation)getHibernateTemplate().get(Relation.class, id);
	}
	
	public List getRelationList(Relation relation){
		String hql="from Relation r";
		if(relation.getUser()!=null){
			hql=hql+" where r.user.id='"+relation.getUser().getId()+"'";
		}
		return getHibernateTemplate().find(hql);
	}
	
    public List getRelationList(){
    	return getHibernateTemplate().find("from Relation Relation");
	}
	
}
